package org.rich.charlesmurphy.ui;

import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import org.ddevil.data.set.DataSet;
import org.rich.charlesmurphy.core.CMIds;
import org.rich.charlesmurphy.model.ResourceStats;
import org.rich.charlesmurphy.ui.renderers.UrlRenderer;

/**
 * @author Rich
 */
public class ViewByResource extends AbstractTableView {
	private static final long serialVersionUID = -3456297118997000135L;

	static String[] IDS = new String[] {
		CMIds.PRE_RESOURCE + CMIds.METHOD,
		CMIds.PRE_RESOURCE + CMIds.PATH,
		CMIds.PARAM_NAMES,
		CMIds.SAMPLE_SIZE,
		CMIds.AVERAGE_SIZE,
		CMIds.MAX_GEN_REQUETS,
		CMIds.DISTINCT_SOURCE_COUNT
	};

	public ViewByResource(String title) {
		super(title);
	}


	/**
	 * {@inheritDoc}
	 */
	@Override
	protected String[] getColumnIds() {
		return IDS;
	}

	@Override
	public Dimension requestDimesions() {
		return new Dimension(525, 500);
	}

	@Override
	protected void initComponents() {
		super.initComponents();
		getTable().setColumnWidths(new int[] { 40, 200, 100, 50, 50, 50, 50});
		getTable().setColumnCellRenderer(CMIds.PRE_RESOURCE + CMIds.PATH, new UrlRenderer());
	}

	@Override
	protected void addListeners() {
		super.addListeners();
		getTable().addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					ResourceStats stats = (ResourceStats) getTable().getSelectedRecord();
					if (stats != null) {
						getControlPanel().launchViewRequestGroupsForResource(stats.getResource());
					}
				}
			}
		});
	}

	public void setModel(DataSet<ResourceStats> model) {
		getTable().setData(model);
	}

}
